<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品型号" prop="cpMode">
        <el-input
          v-model="queryParams.cpMode"
          placeholder="请输入产品型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="客户" prop="client">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.client"-->
      <!--          placeholder="请输入客户"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="产品名称" prop="cpName">
        <el-input
          v-model="queryParams.cpName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="产品数量" prop="scSum">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.scSum"-->
      <!--          placeholder="请输入产品数量"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="单位" prop="unit">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.unit"-->
      <!--          placeholder="请输入单位"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="交货时间" prop="deliveryTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.deliveryTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择交货时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="制单人" prop="zdPerson">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.zdPerson"-->
      <!--          placeholder="请输入制单人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="制单时间" prop="zdTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.zdTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择制单时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="区别码 1-库存备货  2-订单备货" prop="code">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.code"-->
      <!--          placeholder="请输入区别码 1-库存备货  2-订单备货"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!-- 区别码-->
      <el-form-item label="备货类型" prop="code">
        <el-select v-model="queryParams.code" placeholder="请选择备货类型" clearable
                   @keyup.enter.native="handleQuery">
          <el-option label="库存备货" value="1"></el-option>
          <el-option label="订单备货" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['production:orders:add']"
        >新增
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['production:orders:edit']"
        >修改
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['production:orders:export']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--        &lt;!&ndash;          type="danger"&ndash;&gt;-->
    <!--        &lt;!&ndash;          plain&ndash;&gt;-->
    <!--        &lt;!&ndash;          icon="el-icon-delete"&ndash;&gt;-->
    <!--        &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--        &lt;!&ndash;          :disabled="multiple"&ndash;&gt;-->
    <!--        &lt;!&ndash;          @click="handleDelete"&ndash;&gt;-->
    <!--        &lt;!&ndash;          v-hasPermi="['production:orders:remove']"&ndash;&gt;-->
    <!--        &lt;!&ndash;        >删除&ndash;&gt;-->
    <!--        &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="生产工单id" align="center" prop="ordersId"/>-->
      <el-table-column label="产品名称" align="center" prop="cpName" width="300" show-overflow-tooltip/>
      <el-table-column label="产品型号" align="center" prop="cpMode" width="100" show-overflow-tooltip/>
      <el-table-column label="客户" align="center" prop="client" width="300" show-overflow-tooltip/>
      <el-table-column label="产品数量" align="center" prop="scSum" width="80"/>
      <el-table-column label="产品单位" align="center" prop="unit" width="80"/>
      <!--      <el-table-column label="交货时间" align="center" prop="deliveryTime" width="120">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="制单时间" align="center" prop="zdTime" width="120">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.zdTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="备货类型" align="center" prop="code" width="80">
        <template slot-scope="scope">
          {{ scope.row.code == 1 ? '库存备货' : '订单备货' }}
        </template>
      </el-table-column>
      <el-table-column label="制单人" align="center" prop="zdPerson" width="80"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['production:orders:edit']"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['production:orders:remove']"-->
          <!--          >删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="cpName">
              <el-input v-model="form.cpName" placeholder="请输入产品名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品数量" prop="">
              <el-input v-model="form.scSum" placeholder="请输入产品数量" oninput="value=value.replace(/[^\d.]/g,'')"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备货类型" prop="code">
              <el-select @change="giabian" v-model="form.code" placeholder="请选择备货类型" clearable
                         style="width: 100%"
                         @keyup.enter.native="handleQuery">
                <el-option label="库存备货" value="1"></el-option>
                <el-option label="订单备货" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品型号" prop="cpMode">
              <el-input v-model="form.cpMode" placeholder="请输入产品型号"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="客户" id="aaa" prop="client">
              <el-input v-model="form.client" placeholder="请输入客户"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入产品单位"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="交货时间" prop="deliveryTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.deliveryTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择交货时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
        <!--        <el-form-item label="制单人" prop="zdPerson">-->
        <!--          <el-input v-model="form.zdPerson" placeholder="请输入制单人"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="制单时间" prop="zdTime">-->
        <!--          <el-date-picker clearable-->
        <!--                          v-model="form.zdTime"-->
        <!--                          type="date"-->
        <!--                          value-format="yyyy-MM-dd"-->
        <!--                          placeholder="请选择制单时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrders, getOrders, delOrders, addOrders, updateOrders} from "@/api/production/orders";
import {getDate} from "@/utils/getTime";

export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产工单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        cpMode: null,
        client: null,
        cpName: null,
        scSum: null,
        unit: null,
        deliveryTime: null,
        zdPerson: null,
        zdTime: null,
        code: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cpName: [
          {required: true, trigger: "blur", message: "请输入产品名称"}
        ],
        number: [
          {required: true, trigger: "blur", message: "请输入产品数量"}
        ],
      },

    };
  },
  created() {
    this.getList();

    console.log(this.$store.state.user.name)
  },
  methods: {
    giabian() {
      console.log(this.form.code)
      if (this.form.code == "1") {
        let aaa = document.getElementById('aaa')
        console.log(aaa)
        aaa.style.display = "none"
      } else {
        let aaa = document.getElementById('aaa')
        console.log(aaa)
        aaa.style.display = "block"
      }
    },
    /** 查询生产工单列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ordersId: null,
        cpMode: null,
        client: null,
        cpName: null,
        scSum: null,
        unit: null,
        deliveryTime: null,
        zdPerson: null,
        zdTime: null,
        code: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ordersId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产工单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ordersId = row.ordersId || this.ids
      getOrders(ordersId).then(response => {
        this.form = response.data;
        this.form.zdTime = getDate()
        this.open = true;
        this.title = "修改生产工单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.zdTime = getDate()
          if (this.form.ordersId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.zdTime = getDate()
            this.form.zdPerson = this.$store.state.user.name
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ordersIds = row.ordersId || this.ids;
      this.$modal.confirm('是否确认删除生产工单编号为"' + ordersIds + '"的数据项？').then(function () {
        return delOrders(ordersIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('production/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
